<?php
//服务
namespace App\Http\Controllers\Web;

//引用
use App\Http\Controllers\Controller;
use App\Models\Comment;
use App\Models\Order;
use App\Models\Shop;
use App\Models\User;
use App\Repositories\AddressRepository;
use App\Repositories\CommentRepository;
use App\Tools\Map;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class CommentController extends Controller
{

    // 获取商家所有评论
    public function shopComments(Request $request){
        $rules = [
            'shop_id'=>'required|integer|exists:shops,id',
            'type'=>'integer|in:1,2,3'
        ];
        $message = [
            'shop_id.*' => '商家信息有误',
            'type.*'=>'选择正确的评论类型'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(201,$validator->errors()->first());
        }
        $shop_id = request('shop_id',$request->shop_id);
        $type = $request->type;
        if (isset($type)){
            if ($type=='1'){        // 好评
                $res = \App\Models\Comment::with(['users'=>function($query){
                    $query->select('id','name','avatar');
                }])->where('shop_id',$shop_id)->where('score','>','3')->paginate(15);
            }elseif ($type == '2'){
                $res = \App\Models\Comment::with(['users'=>function($query){
                    $query->select('id','name','avatar');
                }])->where('shop_id',$shop_id)->where('score','<','4')->paginate(15);
            }elseif ($type == '3'){
                $res = \App\Models\Comment::with(['users'=>function($query){
                    $query->select('id','name','avatar');
                }])->where('shop_id',$shop_id)
                    ->where('thumb','!=','NULL')
                    ->paginate(15);
            }

        }else{
            $res = \App\Models\Comment::with(['users'=>function($query){
                $query->select('id','name','avatar');
            }])->where('shop_id',$shop_id)->orderBy('score','desc')->paginate(15);
        }
       if ($res->isEmpty()){
            return show(202,'empty');
       }
        return show(200,'ok',$res);
    }

    public function addComment(Request $request){
        $rules = [
            'shop_id'=>'required|integer|exists:shops,id',
            'order_id'=>'required|integer|exists:orders,id',
            'content'=>'required|min:5|string',
            'score'=>'required',
            'thumb'=>'string'
        ];
        $message = [
            'shop_id.*' => '商家信息有误',
            'order_id.*'=>'订单不存在',
            'thumb.*'=>'评价图有误',
            'score.*'=>'评分有误',
            'content.*'=>'评论内容太少'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(201,$validator->errors()->first());
        }
//        $shop_id = $request->shop_id;
        $user_id = Auth::id();
        if (!$user_id){
            return redirect('/login');
        }
        $order_id = $request->order_id;
        $comment = Comment::where('order_id',$order_id)->first();
        if ($comment){
            return show(202,'您评论过该订单');
        }
        $thumb = $request->thumb;
        if (isset($thumb)){
            $data = array_merge(
                \request(['shop_id','order_id','content','score','thumb']),
                compact('user_id')
            );
        }else{
            $data = array_merge(
                \request(['shop_id','order_id','content','score']),
                compact('user_id')
            );
        }
        try{
            Comment::create($data);
            // 更新订单状态
            Order::where('id',$request->order_id)->update(['order_status'=>5]);
            // 给积分 一元换一积分
            $order_price = Order::where('id',6)->first(['order_price']);
            $points = round($order_price->order_price);
            User::where('id',$user_id)->increment("point",$points);
        }catch (\Exception $e){
//            dd($e->getMessage());
            return show(202,'评论失败');
        }
        return show(200,'评论成功');
    }
}
